package com.tool.grab.vpn.task;

import android.net.VpnService.Builder;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import com.tool.grab.R;
import com.tool.grab.Constant;
import com.tool.grab.vpn.HttpVPNService;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ConnectTask implements Runnable {
    private final static String TAG = ConnectTask.class.getSimpleName();
    private HttpVPNService service;
    private long vpnStartTimeLong;
    private String vpnStartTimeString;
    private boolean isRun;
    private byte[] bytes = new byte[Constant.MUTE_SIZE];

    public ConnectTask (HttpVPNService service){
        this.service = service;
    }

    @Override
    public void run() {

        synchronized (this){
            while (service.prepare(service) != null){
                try {
                    wait(250);
                } catch (InterruptedException e) {}
            }
        }

        while (isRun){
            startStream(establishVPN());
        }
    }

    /**
     * 初始化代理 建立连接
     * @return
     */
    private ParcelFileDescriptor establishVPN(){
        vpnStartTimeLong = System.currentTimeMillis();
        vpnStartTimeString = "";

        Builder builder = service.builder();
        builder.setSession(service.getString(R.string.app_name));
        builder.addAddress(Constant.ADDRESS.Address, Constant.ADDRESS.port);
        builder.addRoute(Constant.ROUTE_HOST, Constant.ROUTE_PORT);
        builder.addDnsServer(Constant.GOOGLE_DNS_FIRST);
        builder.addDnsServer(Constant.CHINA_DNS_FIRST);
        builder.addDnsServer(Constant.GOOGLE_DNS_SECOND);
        builder.addDnsServer(Constant.AMERICA);
        builder.setMtu(Constant.MUTE_SIZE);

        String selectPackage = Constant.getPackageName(service);
        if (selectPackage != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                try {
                    builder.addAllowedApplication(selectPackage);
                    builder.addAllowedApplication(service.getPackageName());
                }catch (Exception e){
                    Log.e(TAG,"VPN添加可访问应用出错");
                }
            }
        }
        return builder.establish();
    }

    /**
     * 开启读写流 进行读写操作
     * @param descriptor
     */
    private void startStream(ParcelFileDescriptor descriptor){

        FileOutputStream outputStream = null;
        FileInputStream inputStream = null;
        int size = 0;
        try {
            outputStream = new FileOutputStream(descriptor.getFileDescriptor());
            inputStream = new FileInputStream(descriptor.getFileDescriptor());

            while (size != -1 && isRun){
                boolean write = false;

                if((size = inputStream.read(bytes)) > 0){

                }

            }

        }catch (IOException e){
            if(outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e1) {}
            }
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e1) {}
            }
            isRun = false;
        }


    }
}
